﻿using System.Configuration;
using System.Web;
using System.Web.Http;
using Common.Logging;
using Senparc.Weixin.QY.CommonAPIs;
using Senparc.Weixin.QY.Containers;
using Senparc.Weixin.QY.Helpers;
using User.Api.Dto;

namespace User.Api.Controllers
{
    public class WxJSSDKController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(WxJSSDKController));

        //与企业号公众账号后台的AppId设置保持一致，区分大小写。
        public static readonly string appId = ConfigurationManager.AppSettings["WeixinAppId"];
        //企业号密钥
        private string secret = ConfigurationManager.AppSettings["WeixinAppSecret"];

        /// <summary>
        /// 用户发送消息后，微信平台自动Post一个请求到这里，并等待响应XML。
        /// </summary>
        [AllowAnonymous]
        public JSSDKResponse GetJSSDK()
        {
            //获取时间戳
            var timestamp = JSSDKHelper.GetTimestamp();
            //获取随机码
            var nonceStr = JSSDKHelper.GetNoncestr();
            string ticket = JsApiTicketContainer.TryGetTicket(appId, secret);
            string url = "http://wx.fanthink.com/gysyqy/h5/index.html";
            //获取签名
            var signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, url);

            return new JSSDKResponse
            {
                ResultCode = "0",
                ResultMsg = "",
                AppId = appId,
                JsTicket = ticket,
                Url = url,
                TimeStamp = timestamp,
                NonceStr = nonceStr,
                Signature = signature
            };

        }
    }
}
